using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Speedball2 {
  public class MyRandom {
    public long _unk01;
    public long _unk02;
    public long _unk03;

    public MyRandom () {
      // RAm: 0x14930
      _unk01 = 0x31415926;
      _unk02 = 0x53589793;
      _unk03 = _unk01;
    }

    public MyRandom (long a, long b) {
      _unk01 = a;
      _unk02 = b;
      _unk03 = _unk01;
    }

    public long Next () {
      int d0A = (int) (_unk01 >> 16) & 0xFFFF;
      int d0B = (int) (_unk01 >> 00) & 0xFFFF;
      int d1A = (int) (_unk02 >> 16) & 0xFFFF;
      int d1B = (int) (_unk02 >> 00) & 0xFFFF;
      d0B <<= 1;
      bool extended = (d0B & 0xFFFF0000) != 0;
      d0B &= 0xFFFF;
      int d2B = d0B & 0xFFFF;
      if (!extended)
        d0B += d1B;
      else
        d0B += d1B + 1;
      extended = (d0B & 0xFFFF0000) != 0;
      d0B &= 0xFFFF;
      d1B = d2B;
      //extended = false;
      ExtensionMethods.Swap (ref d0A, ref d0B);
      ExtensionMethods.Swap (ref d1A, ref d1B);
      d2B = d0B;
      if (!extended)
        d0B += d1B;
      else
        d0B += d1B + 1;
      extended = (d0B & 0xFFFF0000) != 0;
      d0B &= 0xFFFF;
      d1B = d2B;
      ExtensionMethods.Swap (ref d0A, ref d0B);
      ExtensionMethods.Swap (ref d1A, ref d1B);
      _unk01 = (((d0A & 0xFFFF) << 16) | (d0B & 0xFFFF)) & 0xFFFFFFFF;
      _unk02 = (((d1A & 0xFFFF) << 16) | (d1B & 0xFFFF)) & 0xFFFFFFFF;
      _unk03 = _unk01;

      return (_unk03);
    }

    public int NextByte () {
      return ((int) (Next () & 0xFF));
    }

    public int NextByte (int mask) {
      return (NextByte () & mask);
    }

    public int NextWord () {
      return ((int) (Next () & 0xFFFF));
    }

    public int NextWord (int mask) {
      return (NextWord () & mask);
    }

    public void Dump () {
      Console.WriteLine ("{0:X8} {1:X8}", _unk01, _unk02);
    }
  }
}
